package com.zenithmind.library.pojo.vo;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * 借阅记录视图对象
 * 
 * @author ZenithMind
 * @since 2024-06-14
 */
@Data
@Schema(name = "借阅记录视图对象", description = "借阅记录信息的视图对象")
public class BorrowRecordVO {

    @Schema(description = "借阅记录ID")
    private String id;

    @Schema(description = "借阅用户ID")
    private String userId;

    @Schema(description = "借阅用户姓名")
    private String userName;

    @Schema(description = "用户学号/工号")
    private String userNumber;

    @Schema(description = "图书副本ID")
    private String bookCopyId;

    @Schema(description = "图书ID")
    private String bookId;

    @Schema(description = "图书标题")
    private String bookTitle;

    @Schema(description = "图书ISBN")
    private String bookIsbn;

    @Schema(description = "图书作者")
    private String bookAuthor;

    @Schema(description = "图书封面")
    private String bookCover;

    @Schema(description = "条形码")
    private String barcode;

    @Schema(description = "借阅日期")
    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private LocalDate borrowDate;

    @Schema(description = "应还日期")
    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private LocalDate dueDate;

    @Schema(description = "实际归还日期")
    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private LocalDate returnDate;

    @Schema(description = "借阅状态")
    private Integer status;

    @Schema(description = "借阅状态名称")
    private String statusName;

    @Schema(description = "续借次数")
    private Integer renewalCount;

    @Schema(description = "最大续借次数")
    private Integer maxRenewalCount;

    @Schema(description = "逾期天数")
    private Integer overdueDays;

    @Schema(description = "罚金金额")
    private BigDecimal fineAmount;

    @Schema(description = "是否已支付罚金")
    private Integer finePaid;

    @Schema(description = "借阅操作员ID")
    private String borrowOperatorId;

    @Schema(description = "借阅操作员姓名")
    private String borrowOperatorName;

    @Schema(description = "归还操作员ID")
    private String returnOperatorId;

    @Schema(description = "归还操作员姓名")
    private String returnOperatorName;

    @Schema(description = "借阅备注")
    private String borrowRemarks;

    @Schema(description = "归还备注")
    private String returnRemarks;

    @Schema(description = "提醒次数")
    private Integer reminderCount;

    @Schema(description = "最后提醒时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime lastReminderTime;

    @Schema(description = "创建时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createTime;

    @Schema(description = "更新时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime updateTime;

    /**
     * 获取借阅状态名称
     */
    public String getStatusName() {
        if (status == null) {
            return "未知";
        }
        return switch (status) {
            case 0 -> "借阅中";
            case 1 -> "已归还";
            case 2 -> "逾期";
            case 3 -> "已续借";
            case 4 -> "丢失";
            case 5 -> "损坏";
            default -> "未知";
        };
    }

    /**
     * 判断是否逾期
     */
    public boolean isOverdue() {
        if (returnDate != null) {
            return false; // 已归还
        }
        return dueDate != null && LocalDate.now().isAfter(dueDate);
    }

    /**
     * 计算剩余天数
     */
    public int getRemainingDays() {
        if (returnDate != null || dueDate == null) {
            return 0;
        }
        return (int) java.time.temporal.ChronoUnit.DAYS.between(LocalDate.now(), dueDate);
    }

    /**
     * 判断是否可以续借
     */
    public boolean canRenew() {
        return status != null && 
               (status == 0 || status == 3) && // 借阅中或已续借
               (renewalCount == null || renewalCount < maxRenewalCount) &&
               !isOverdue();
    }

    /**
     * 判断是否需要提醒
     */
    public boolean needReminder() {
        if (returnDate != null || dueDate == null) {
            return false;
        }
        int remainingDays = getRemainingDays();
        return remainingDays <= 3 && remainingDays >= 0;
    }
}
